package com.jy.modules.cims.component.sysAssist;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.exception.SysAssistException;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.data.common.dao.TCMmCfgDealCodeDao;
import com.jy.modules.cims.data.common.dao.TCMmDueexecutetaskcfgDao;
import com.jy.modules.cims.data.dao.InteractClassParamterDao;
import com.jy.modules.cims.data.dao.SysAssistDao;
import com.jy.modules.cims.data.dto.CfgDealCodeDto;
import com.jy.modules.platform.sysconfig.service.SysConfigService;
import com.jy.platform.tools.common.DateUtil;

/**
 * 系统辅助组件服务实现
 */
@Service("com.jy.modules.cims.component.sysAssist.CSysAssistImpl")
public class CSysAssistImpl implements ICSysAssist {
	private static final Logger logger = LoggerFactory.getLogger(CSysAssistImpl.class);

	@Autowired
	public TCMmCfgDealCodeDao tcmmCfgDealCodeDao;

	@Autowired
	public TCMmDueexecutetaskcfgDao tcmmDueexecutetaskcfgDao;

	@Autowired
	public InteractClassParamterDao interactClassParamterDao;

	@Autowired
	public SysAssistDao sysAssistDao;

	@Autowired
	public SpringContextUtils springContextUtil;

	@Autowired
	@Qualifier("com.jy.modules.platform.sysconfig.service.SysConfigService")
	private SysConfigService sysConfigService;

	public CSysAssistImpl() {
	}

	private String fullStreamNo(Long streamNo, int len) {
		StringBuffer strStreamNo = new StringBuffer();
		int oldLen = streamNo.toString().length();
		for (int i = 0; i < len - oldLen; i++) {
			strStreamNo = strStreamNo.append("0");
		}
		strStreamNo = strStreamNo.append(streamNo);
		return strStreamNo.toString();
	}

	/**
	 * 根据业务类型生成业务单号 采用默认规则生成，后续有个性化差异的时候再调整 格式：zz20150101000001
	 * 
	 * @param type
	 * @return
	 */
	public String getBizNo(String type) throws SysAssistException {
		type = CimsConstant.map.get(type);
		String dealDate = DateUtil.format(new Date(), "yyyyMMdd");
		int streamLen = 6;
		Long streamNo = 1L;

		CfgDealCodeDto queryDto = new CfgDealCodeDto();
		queryDto.setDealCodeType(type);
		queryDto.setDealCodeDate(dealDate);

		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams.put("dto", queryDto);
			List<CfgDealCodeDto> list = sysAssistDao.searchCfgDealCodeForUpdate(searchParams);

			if (list == null || list.size() == 0) { // 新增流水记录
				queryDto.setDealCodeValue(streamNo);
				searchParams = new HashMap<String, Object>();
				searchParams.put("dto", queryDto);
				tcmmCfgDealCodeDao.insertTCMmCfgDealCode(searchParams);
			} else { // 流水+1处理
				CfgDealCodeDto result = list.get(0);
				result.setDealCodeValue(result.getDealCodeValue() + 1);
				streamNo = result.getDealCodeValue();
				searchParams = new HashMap<String, Object>();
				searchParams.put("dto", result);
				tcmmCfgDealCodeDao.updateTCMmCfgDealCode(searchParams);
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new SysAssistException(CimsExceptionConstant.SYSASSIST_CREATE_BIZNO_FAIL);
		}
		return type + dealDate + fullStreamNo(streamNo, streamLen);
	}

	public String getSysConfigValueByCode(String code) {
		String value = "";
		try {
			value = sysConfigService.queryValueByCode(code);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}

		return value;
	}

	/**
	 * select 
	 * @param params
	 * @return
	 */
	public List<Map<String, Object>> querySql(String sql) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sql", sql);
		return sysAssistDao.querySql(params);
	}

	/**
	 * update/delete/insert/alert/.....
	 * @param params
	 */
	public void executeSql(String sql) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sql", sql);
		sysAssistDao.executeSql(params);
	}

	/**
	 * 
	 * @param params
	 */
	public void insertTable(String sql) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sql", sql);
		sysAssistDao.insertTable(params);
	}

}